/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain an copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.fourmob.datepicker.slice;

import com.fourmob.datepicker.ResourceTable;
import com.fourmob.datepicker.ToastsUtils;
import com.fourmob.datetimepicker.date.DatePickerDialog;
import com.sleepbot.datetimepicker.time.RadialPickerLayout;
import com.sleepbot.datetimepicker.time.TimePickerDialog;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Color;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Calendar;

/**
 * @since 2021-04-06
 */
public class SecondAbilitySlice extends AbilitySlice
        implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
    public static final int TWO_THOUSAND = 2000;
    public static final String DATEPICKER_TAG = "datepicker";
    public static final String TIMEPICKER_TAG = "timepicker";

    private static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x000110, "DatePickerDialogCom");

    final Calendar calendar = Calendar.getInstance();
    private TimePickerDialog timePickerDialog;
    private DatePickerDialog datePickerDialog;
    private DirectionalLayout ability_Second_Direc;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);
        getWindow().setStatusBarColor(Color.getIntColor("#222222"));

        datePickerDialog = DatePickerDialog.newInstance(this,SecondAbilitySlice.this,
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH), isVibrate());
        timePickerDialog = TimePickerDialog.newInstance((TimePickerDialog.OnTimeSetListener) this,
            SecondAbilitySlice.this,
            calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),
            false, false);
        ability_Second_Direc = (DirectionalLayout) findComponentById(ResourceTable.Id_ability_second_Direc);

        findComponentById(ResourceTable.Id_dateButton).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ability_Second_Direc.setFadeEffectColor(Color.BLUE);
                showdate();
            }
        });

        findComponentById(ResourceTable.Id_timeButton).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
              showtime();
            }
        });
    }

    private void showdate() {
        datePickerDialog = DatePickerDialog.newInstance(this,SecondAbilitySlice.this,
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
            isVibrate());
        HiLog.info(label,"showdate0");
        datePickerDialog.setVibrate(isVibrate());
        HiLog.info(label,"showdate");
        datePickerDialog.setYearRange(1985,2028);
        HiLog.info(label,"showdate1");
        datePickerDialog.setCloseOnSingleTapDay(isCloseOnSingleTapDay());
        HiLog.info(label,"showdate2");
        datePickerDialog.setOnDateSetListener(this);
        HiLog.info(label,"showdate3");
        datePickerDialog.show();
    }

    private void showtime() {
        timePickerDialog = TimePickerDialog.newInstance((TimePickerDialog.OnTimeSetListener) this,
            SecondAbilitySlice.this, calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE), false, false);
        timePickerDialog.setVibrate(isVibrate());
        timePickerDialog.setCloseOnSingleTapMinute(isCloseOnSingleTapMinute());
        timePickerDialog.setOnTimeSetListener(this);
        timePickerDialog.show();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        new ToastsUtils.Builder(getContext())
            .setToastText("new time:" + hourOfDay + "-"
                + minute).setDuration(2000)
            .build().show();
    }

    private boolean isVibrate() {
        return ((Checkbox) findComponentById(ResourceTable.Id_checkBoxVibrate)).isChecked();
    }

    private boolean isCloseOnSingleTapDay() {
        return ((Checkbox) findComponentById(ResourceTable.Id_checkBoxCloseOnSingleTapDay)).isChecked();
    }

    private boolean isCloseOnSingleTapMinute() {
        return ((Checkbox) findComponentById(ResourceTable.Id_checkBoxCloseOnSingleTapMinute)).isChecked();
    }

    @Override
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
        new ToastsUtils.Builder(getContext())
            .setToastText("new date:"
                + year + "-" + (month + 1) + "-" + day).setDuration(2000)
            .build().show();
    }
}
